package sy.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import sy.pageModel.DataGrid;
import sy.pageModel.DeptPic;
import sy.pageModel.Json;
import sy.service.PicServiceI;
import sy.util.StringUtil;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * Created by Administrator on 2016-06-29.
 */
@Controller
@RequestMapping("picController")
public class PicController extends BaseController {
    @Autowired
    private PicServiceI picService;

    @Autowired
    private HttpSession session;
    @RequestMapping("/datagrid.action")
    @ResponseBody
    public DataGrid findPicAction(DeptPic pic){
        return picService.findPicDataGird(pic);
    }

    @RequestMapping("/find_code.action")
    @ResponseBody
    public Json findAction(String types){
        Json js=new Json();
        js.setObj(findOption(picService.findCodeList(types)));
        return js;
    }
    @RequestMapping("/add_pic.action")
    @ResponseBody
    public Json addPicAction(DeptPic pic){
        int rows=0;
        Map map=StringUtil.findMap(pic.getPic());
        pic.setPic((String) map.get("pic"));
        pic.setId((String) map.get("id"));
        try {
            rows=picService.addPicService(pic);
            if (rows>0)  logUserAction(session,"picController/add_pic.action",pic.toString());
        } catch (Exception e) {
            e.printStackTrace();
            rows=0;
        }
        Json js= StringUtil.findJson(rows,"添加");
        return js;
    }
    @RequestMapping("/edit_pic.action")
    @ResponseBody
    public Json editPicAction(DeptPic pic){
        int rows=0;
        pic.setPic(StringUtil.findStr(pic.getPic()));
        try {
             rows=picService.editPicService(pic);
            if (rows>0)  logUserAction(session,"picController/edit_pic.action",pic.toString());
        } catch (Exception e) {
            e.printStackTrace();
            rows=0;
        }
        Json js= StringUtil.findJson(rows,"修改");
        return js;
    }


    @RequestMapping("/remove_pic.action")
    @ResponseBody
    public Json removePicAction(DeptPic pic){
        int rows=0;
        try {
            rows= picService.deletePicService(pic.getIds());
            if (rows>0)  logUserAction(session,"picController/remove_pic.action",pic.toString());
        } catch (Exception e) {
            e.printStackTrace();
            rows=0;
        }
        Json js= StringUtil.findJson(rows,"删除");
        return js;
    }
}